package com.xinyi.reactivemvvmkt.ui.fragment

import android.view.View
import androidx.fragment.app.viewModels
import com.xinyi.basic.utils.LogUtil
import com.xinyi.reactivemvvmkt.R
import com.xinyi.reactivemvvmkt.base.fragmet.BaseViewBindingFragment
import com.xinyi.reactivemvvmkt.databinding.FragmentHomeBinding
import com.xinyi.reactivemvvmkt.model.Resource
import com.xinyi.reactivemvvmkt.model.request.NeteaseCommentRequest
import com.xinyi.reactivemvvmkt.viewmodel.HomeViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 首页Fragment
 * @author 杨耿雷
 * @since 2025/2/20 14:46
 */
@AndroidEntryPoint
class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClickListener {

    private val mHomeViewModel: HomeViewModel by viewModels()

    companion object {
        fun newInstance(): HomeFragment {
            return HomeFragment()
        }
    }

    override fun initLayoutId(): Int {
        return R.layout.fragment_home
    }

    override fun initObserveUI() {
        super.initObserveUI()
        // 观察网易云评论接口
        mHomeViewModel.mNeteaseComment.observe(this) {
            when (it) {
                is Resource.Loading -> {
                    // 加载中
                    binding.tvIvCommentTxt.text = getString(R.string.loading)
                }

                is Resource.Success -> {
                    // 加载成功
                    binding.tvIvCommentTxt.text = it.value.content
                }

                is Resource.Fail -> {
                    // 加载失败
                    LogUtil.e(it.ex)
                }
            }
        }
    }

    override fun initListeners() {
        super.initListeners()
        binding.tvRefresh.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_refresh -> {
                // 请求网易云评论接口
                mHomeViewModel.requestNeteaseComment(
                    NeteaseCommentRequest(
                        format = "json",
                    )
                )
            }
        }
    }
}